﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using InHD.Properties;

namespace InHD.utilities
{
    class SingleInstanceApplicationWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public SingleInstanceApplicationWrapper()
        {
            // Enable single-instance mode.
            this.IsSingleInstance = true;
        }

        // Create the WPF application class.
        private InhdApp app;
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            try
            {
                SplashScreen splashScreen = new SplashScreen("media/images/loading.png");
                // Show the splash screen.
                // The true parameter sets the splashScreen to fade away automatically
                // after the first window appears.
                splashScreen.Show(true);
            }
            catch (Exception ex)
            {
                Logger.RecordMessage(ex, MessageType.Error);
            }

            app = new InhdApp();
            app.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(app_DispatcherUnhandledException);
            app.Run();
            return false;
        }

        void app_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Logger.RecordMessage(e.Exception, MessageType.Failure);
            MessageBox.Show(Resources.InHD_Exception, Resources.App_Title, MessageBoxButton.OK);
            e.Handled = true;
        }

        // Direct multiple instances.
        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e)
        {
            base.OnStartupNextInstance(e);
            app.MainUI.Activate();
        }
    }
}
